package com.da.web.school_collage.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.da.common.Result;
import com.da.web.school_collage.entity.ListParm;
import com.da.web.school_collage.entity.SchoolCollage;
import com.da.web.school_collage.service.SchoolCollageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author da
 * @time 2023/12/5 上午 8:00
 */
@RestController
@RequestMapping("/api/collage")
public class SchoolCollageController {
    @Autowired
    private SchoolCollageService schoolCollageService;

    //新增
    @PostMapping
    public Result add(@RequestBody SchoolCollage schoolCollage) {
        //设置创建时间
        schoolCollage.setCreateTime(new Date());
        //插入数据
        boolean save = schoolCollageService.save(schoolCollage);
        if (save) {
            return Result.success("新增学院成功");
        }
        return Result.error("新增学院失败");
    }

    //编辑
    @PutMapping
    public Result edit(@RequestBody SchoolCollage schoolCollage) {
        //设置创建时间
        schoolCollage.setUpdateTime(new Date());
        //插入数据
        boolean save = schoolCollageService.updateById(schoolCollage);
        if (save) {
            return Result.success("编辑学院成功");
        }
        return Result.error("编辑学院失败");
    }

    //删除
    @DeleteMapping("/{collageId}")
    public Result delete(@PathVariable("collageId") Long collageId) {
        boolean b = schoolCollageService.removeById(collageId);
        if (b) {
            return Result.success("删除学院成功");
        }
        return Result.error("删除学院失败");
    }

    //列表
    @GetMapping("/list")
    public Result getList(ListParm parm) {
        //查询条件
        QueryWrapper<SchoolCollage> query = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(parm.getCollageName())) {
            query.lambda().like(SchoolCollage::getCollageName, parm.getCollageName());
        }
        //构造分页对象
        IPage<SchoolCollage> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        IPage<SchoolCollage> list = schoolCollageService.page(page, query);
        return Result.success("查询成功", list);
    }


}
